package com.health.web.controller.health;

import com.alibaba.druid.support.json.JSONUtils;
import com.health.common.annotation.RateLimiter;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.common.enums.LimitType;
import com.health.system.domain.HealthCardInfoDTE;
import com.health.system.domain.vo.HealthCardInfoRequestVo;
import com.health.system.service.IHealthService;
import com.health.common.utils.sign.Base64;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/health")
@Api(tags = "健康管理")
@Slf4j
public class HealthController extends BaseController {

    @Resource
    private IHealthService healthService;

    /**
     * 身份证号码脱敏处理
     * @param idCard 身份证号码
     * @return 脱敏后的身份证号码
     */
    private String desensitizeIdCard(String idCard) {
        if (idCard == null || idCard.length() < 8) {
            return idCard;
        }
        return idCard.substring(0, 4) + "********" + idCard.substring(idCard.length() - 4);
    }

    @GetMapping("/info")
    @RateLimiter(key = "info", count = 10, limitType = LimitType.IP)
    public AjaxResult info(@RequestParam String xm, @RequestParam String sfzhm, @RequestParam String djrq)
    {
        HealthCardInfoRequestVo healthCardInfoRequestVo = new HealthCardInfoRequestVo();
        healthCardInfoRequestVo.setXm(xm);
        healthCardInfoRequestVo.setSfzhm(sfzhm);
        healthCardInfoRequestVo.setDjrq(djrq);
        HealthCardInfoDTE healthCardInfoDTE = healthService.selectHealthCardInfo(healthCardInfoRequestVo);
        if(healthCardInfoDTE != null){
            Date registrationDate = healthCardInfoDTE.getDjrq();
            if (registrationDate != null) {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.YEAR, -1);
                Date oneYearAgo = calendar.getTime();
                healthCardInfoDTE.setIsExpire(registrationDate.before(oneYearAgo) ? 1 : 0);
            }
            if (healthCardInfoDTE.getXpxx() != null && healthCardInfoDTE.getXpxx().length > 0) {
                log.info("相片大小：{}", healthCardInfoDTE.getXpxx().length);
                String base64Image = Base64.encode(healthCardInfoDTE.getXpxx());
                log.info("相片base64：{}", base64Image);
                healthCardInfoDTE.setXpxxBase64(base64Image);
            }
            // 对身份证号码进行脱敏处理
            healthCardInfoDTE.setSfzhm(desensitizeIdCard(healthCardInfoDTE.getSfzhm()));
            // 格式化日期为中文格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日");
            String formattedDate = sdf.format(registrationDate);
            healthCardInfoDTE.setDjrqStr(formattedDate);
        }
        return AjaxResult.success(healthCardInfoDTE);
    }
}
